package cn.itcast.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 请求之前执行
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断是否进入登入页面
        if (request.getRequestURI().endsWith("login")) {
            return true;
        }
        //判断session
        HttpSession session = request.getSession();

        if (null != session.getAttribute("member")) {
            return true;
        }
        System.out.println("跳转页面");
        return true;
        //System.out.println(request.getRequestURL());
        //System.out.println(request.getRequestURI());
        //response.setCharacterEncoding("UTF-8");
        //response.sendRedirect("http://localhost:63342/project-practice--diwuzu/itcast-health-parent/itcast-health-mobile-html/pages/login.html?_ijt=gr8fcobkptls51tanac7r7a7g7");

    }

    /**
     * 完成之后执行
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
